package com;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.util.Date;
import java.util.Enumeration;
import java.util.Map;

import com.main;

@WebServlet(name = "loginServlet")
public class loginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("post");
        Users users=new Users(request.getParameter("username"),request.getParameter("password"),request.getParameter("sex"),request.getParameter("email"));
        System.out.println(users.getUsername());
        System.out.println(users.getPassword());
        System.out.println(users.getSex());
        System.out.println(users.getEmail());

    }
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("get");
        ServletContext servletContext=request.getServletContext();
        Map<Connection,Boolean>map= (Map<Connection, Boolean>) servletContext.getAttribute("key1");
        Users users=new Users(request.getParameter("username"),request.getParameter("password"),request.getParameter("sex"),request.getParameter("email"));
        System.out.println(users.getUsername());
        System.out.println(users.getPassword());
        System.out.println(users.getSex());
        System.out.println(users.getEmail());
        Connection connection=null;
            for (Connection c:map.keySet()
            ) {
                if (map.get(c))
                {
                    connection=c;
                    map.replace(c,false);
                    break;

                }
            }
        Date stardata=new Date();
        users.save(connection);
        Date enddata=new Date();
        System.out.println("添加耗时"+(enddata.getTime()-stardata.getTime())+"毫秒");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out=response.getWriter();
        out.write("<font style='color:red'>"+users.getUsername()+"注册成功!!!</font>");
    }
}
